package com.example.toastalterdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AlertDialog alertDialog;
    private AlertDialog.Builder builder;
    private boolean[] checkItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn1 = findViewById(R.id.btn_dialog_one);
        Button btn2 = findViewById(R.id.btn_dialog_two);
        Button btn3 = findViewById(R.id.btn_dialog_three);
        Button btn4 = findViewById(R.id.btn_dialog_four);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_dialog_one:
                alertDialog = null;
                builder = new AlertDialog.Builder(this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("系统提示")
                        .setMessage("这是一个最普通的AlertDialog，\n带有三个按钮，分别是尖，中立和确定")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this, "你点击了确认按钮", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("中立", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this, "你点击了中立按钮", Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                alertDialog.show();
                break;
            case R.id.btn_dialog_two:
                final String[] lesson = new String[]{"语文", "数学", "英语"};
                alertDialog = null;
                builder = new AlertDialog.Builder(this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("选择你喜欢的课程")
                        .setItems(lesson, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(getApplicationContext(), "你选择了" + lesson[i], Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                alertDialog.show();
                break;
                //单选列表对话框
            case R.id.btn_dialog_three:
                final String[] fruits = new String[]{"语文", "数学", "英语"};
                alertDialog = null;
                builder = new AlertDialog.Builder(this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("选择你喜欢的睡过，只能选一个哦")
                        .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(getApplicationContext(), "你选择了" + fruits[i], Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                alertDialog.show();
                break;
            case R.id.btn_dialog_four:
                final String[] menu = new String[]{"语文", "数学", "英语"};
                // 定义一个用来记录列表项状态的boolean数组
                checkItem = new boolean[]{false, false, false};
                alertDialog = null;
                builder = new AlertDialog.Builder(this);
                alertDialog = builder.setIcon(R.mipmap.ic_launcher)
                        .setMultiChoiceItems(menu, checkItem, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {
                                checkItem[which] = isChecked;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int which) {
                                String result = "";
                                for (int i = 0; i < checkItem.length; i++) {
                                    if (checkItem[i]) {
                                        result += menu[i] + " ";
                                    }
                                }
                                Toast.makeText(getApplicationContext(), "你选择了" + result, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setCancelable(false)
                        .create();
                alertDialog.show();
            default:
                break;
        }
    }
}
